package gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import neural_network.CancerTypes;
import neural_network.CommandRunner;

public class StatisticsPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JButton trainAgain, okay;
	JPanel textPanel;

	public StatisticsPanel() {
		BorderedLabel label = new BorderedLabel("Statistika neuralne mreže",
				new Color(0x55acee), 20);

		textPanel = new JPanel();
		textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.PAGE_AXIS));
		fillPanel();
		setVisible(false);
		
		JPanel randompanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		randompanel.add(textPanel);
		this.add(label);
		this.add(randompanel);
		this.trainAgain = new JButton("Treniraj ponovo");
		trainAgain.addActionListener(new TrainAgain());
		this.okay = new JButton("Nastavi dalje");
		okay.addActionListener(new OkayAction());
		this.add(trainAgain);
		this.add(okay);
		
	}

	private void fillPanel() {
		Scanner sc;
		try {
			StringBuilder builder = new StringBuilder();
			builder.append("<html>");
			sc = new Scanner(new File("result_validation.txt"), "UTF-8");
			boolean parni = false;
			while (sc.hasNext()) {
				String line = sc.nextLine();
				int i = 0;
				builder.append("<table cellspacing=\"1px\" cellpadding=1><th colspan=\"2\" align=\"center\" bgcolor=\"#55acee\">" + line
						+ "</th>");
				while (i < 6) {
					line = sc.nextLine();
					String line2 = sc.nextLine();
					String color = parni ? "#FFFFFF" : "#C0C0C0";
					parni = !parni;
					builder.append("<tr bgcolor=\"" + color +"\"><td>" + line + "</td><td>" + line2 + "</td></tr>");
					i++;
				}
				builder.append("</table><br/>");
								
//				builder = new StringBuilder();
			}		
			builder.append("</html>");
			BorderedLabel label = new BorderedLabel(builder.toString(),
					Color.BLACK, 11, SwingConstants.CENTER, 450, 500);
			textPanel.add(label);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public class TrainAgainAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			CommandRunner.runCommand("octave metastaze.m no_pause no_graphic");
			CommandRunner.runCommand("octave karcinom.m no_pause no_graphic");
			clearPanel();
			fillPanel();
		}

		private void clearPanel() {
			StatisticsPanel.this.textPanel.removeAll();
		}
	}

	public class OkayAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			Frame.getFrame().setStatisticPanelVisible(false);
			Frame.getFrame().setReadyStatePanelVisible(true);
		}

	}
	
	public class TrainAgain implements ActionListener{
		

		@Override
		public void actionPerformed(ActionEvent e) {
			Frame.getFrame().setStatisticPanelVisible(false);
			Frame.getFrame().setNotReadyStatePanelVisible(true);		
		}
		
	}
}
